﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccess;
using System.Data;
using BusinessLogic;

namespace Manager
{
    public partial class AccessoryManager : System.Web.UI.Page
    {
        private string text = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            ucPaging.PageChange += new global::Manager.Controls.ucPaging.PagingHandler(Load_Data);
            if (!Page.IsPostBack)
            {
                ucUpdate.IsNew = true;
                ucPaging.PageDisplay = 5;
                ucPaging.PageSize = 20;
                Load_Data();
                
            }
        }

        void Load_()
        {
            throw new NotImplementedException();
        }

        protected void btnNew_Click(object sender, EventArgs e)
        {
            ucUpdate.IsNew = true;
            ucUpdate.Reset_Control();
        }

        #region Load Data
        private void Load_Data()
        {
            int totalRecord = 0;
            AccessoryBusiness accessoryBusiness = new AccessoryBusiness();
            DataTable dtbAccessory = accessoryBusiness.Search(text, ucPaging.CurrentPage, ucPaging.PageSize, ref totalRecord);
            ucPaging.TotalRecord = totalRecord;
            ucPaging.Visible = ucPaging.TotalPage > 1;

            if ((dtbAccessory != null) && (dtbAccessory.Rows.Count > 0))
            {
                rptAccessory.DataSource = dtbAccessory;
                rptAccessory.DataBind();
            }
        }

        protected void rptAccessory_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            int accessoryID = Convert.ToInt32(DataBinder.Eval(e.Item.DataItem, "AccessoryID"));
            AccessoryInfo accessoryInfo = AccessoryData.SelectByAccessoryID(accessoryID);

            Label lblStatus = (Label)e.Item.FindControl("lblStatus");
            Label lblDescription = (Label)e.Item.FindControl("lblDesc");

            string description = accessoryInfo.AccessoryDescription;
            if (description.Length > 200)
            {
                lblDescription.Text = description.Substring(0, 200) + "...";
            }
            else
            {
                lblDescription.Text = description;
            }

            if (accessoryInfo.AccessoryStatus == true)
            {
                lblStatus.Text = "Hiển thị";
            }
            else
            {
                lblStatus.Text = "Không Hiển thị";
            }
        }
        #endregion

        #region rptAccessory_ItemCommand
        protected void rptAccessory_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int accessoryID = Convert.ToInt32(e.CommandArgument);

            if (e.CommandName == "View")
            {
                ucUpdate.Reset_Control();
                ucUpdate.IsNew = false;
                ucUpdate.Load_Accessory(accessoryID);
            }

            if (e.CommandName == "Del")
            {
                AccessoryData.DeleteByAccessoryID(accessoryID);
                Load_Data();
            }
        }
        #endregion

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            text = txtSearch.Text.Trim();
            Load_Data();
        }
    }
}